// pages/login/login.js
import validate from 'wechat-validate'
Page({
  behaviors: [validate],
  data: {
    height: '',
    account: 'hdsj001',
    password: '123456'
  },
  rules: {
    account: [
      { required: true, message: '请输入账号' },
      { min: 6, max: 12, message: '请输入正确的账号' }
    ],
    password: [
      { required: true, message: '请输入密码' },
      { min:4, max: 10, message: '请输入正确的密码' }
    ]
  },
  onLoad({ redirectURL }) {
    this.redirectURL = redirectURL
    const height = wx.getSystemInfoSync()
    this.setData({
      height: height.statusBarHeight
    })
  },
  async login() {
    const validate = this.validate()
    if(!validate) return
    const { account, password } = this.data
    const res = await wx.$request.post('/driver/login/account', { account, password })
    console.log(res)
    const app = getApp()
    app.setToken('token', res)
    const url = this.redirectURL || '/pages/user/user'
    wx.switchTab({
      url
    })
  }
})